package rs.networx.servlets;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rs.networx.dao.FriendRequestDao;
import rs.networx.entity.FriendRequest;
import rs.networx.entity.Person;
import rs.networx.util.ErrorMessages;

public class ViewFriendRequestsLoggedUserServlet extends HttpServlet {

	private static final long serialVersionUID = -7222678996567968231L;
	
	@EJB
	private FriendRequestDao friendRequestDao;
	
	private String username;
	private String password;
	private Integer intLoggedUserID;
	private Person loggedUser;
	
	private List<FriendRequest> friendRequests;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		System.out.println("LOADED: ViewFriendRequestsLoggedUserServlet");
		
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());
		
		HttpSession session = request.getSession();
		loggedUser = (Person)session.getAttribute("logovaniUser");
		
		try {	
			username = loggedUser.getEmail();
			password = loggedUser.getPassword();
			intLoggedUserID = loggedUser.getId();
			System.out.println(username + "  " + password + "  " + intLoggedUserID);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		if(loggedUser != null){
			friendRequests = friendRequestDao.getAllFriendRequestsForLoggedUser(loggedUser);
			if(friendRequests != null && friendRequests.size() > 0){
				session.setAttribute("searchStatus", ErrorMessages.PERSON_HAVE_FRIEND_REQUESTS);	
				session.setAttribute("friendRequests", friendRequests);
			}else if(friendRequests == null || friendRequests.size() == 0){
				session.setAttribute("searchStatus", ErrorMessages.PERSON_DO_NOT_HAVE_FRIEND_REQUESTS);
			}
			request.getRequestDispatcher("ViewFriendsRequests.jsp").forward(request, response);
			return;
		}else if(loggedUser == null){
			request.getRequestDispatcher("Welcome.jsp").forward(request, response);
			return;
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doGet(request, response);
	}
	

}
